package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.materialClassificationTypeCommand.AddMaterialClassificationTypeCommand;
import com.woniu.command.materialClassificationTypeCommand.DeleteMaterialClassificationTypeCommand;
import com.woniu.command.materialClassificationTypeCommand.UpdateMaterialClassificationTypeCommand;
import com.woniu.inlet.vo.MaterialClassificationTypeVo;
import com.woniu.outlet.po.MaterialClassificationTypePo;
import com.woniu.service.MaterialClassificationTypeService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "物料分类管理")
@Slf4j
@RequestMapping("/materialClassificationType/")
public class MaterialClassificationTypeController {

    @Autowired
    private MaterialClassificationTypeService materialClassificationTypeService;

    @GetMapping("/materialClassificationTypes")
    @ApiOperation("查询物料分类")
    public ResponseResult<List<MaterialClassificationTypeVo>> findMaterialClassificationTypes() throws Exception{
        List<MaterialClassificationTypePo>  materialClassificationTypePos =  materialClassificationTypeService.findMaterialClassificationTypes();
        List<MaterialClassificationTypeVo> materialClassificationTypeVos = new ArrayList<>();
        //对象的深拷贝
        for (MaterialClassificationTypePo item : materialClassificationTypePos){
            MaterialClassificationTypeVo materialClassificationTypeVo = new MaterialClassificationTypeVo();
            BeanUtils.copyProperties(item,materialClassificationTypeVo);
            materialClassificationTypeVos.add(materialClassificationTypeVo);
        }
        return new ResponseResult<>(materialClassificationTypeVos,"查询成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/materialClassificationTypes/id")
    @ApiOperation("根据id查询物料分类")
    public ResponseResult<MaterialClassificationTypeVo> getMaterialClassificationTypeById(Long id) throws Exception {
        MaterialClassificationTypePo materialClassificationTypePo = materialClassificationTypeService.getMaterialClassificationTypeById(id);

        //对象的深拷贝

        MaterialClassificationTypeVo materialClassificationTypeVo = new MaterialClassificationTypeVo();
        BeanUtils.copyProperties(materialClassificationTypePo, materialClassificationTypeVo);


        return new ResponseResult<>(materialClassificationTypeVo,"查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加物料分类")
    @ApiOperationSupport(ignoreParameters = {"id","classificationCode","spare1","spare2","spare3"})
    public ResponseResult<String> addMaterialClassificationType(MaterialClassificationTypePo materialClassificationTypePo){
        Command command = new AddMaterialClassificationTypeCommand();
        command.execute(materialClassificationTypePo);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改物料分类")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateMaterialClassificationType(MaterialClassificationTypePo materialClassificationTypePo){
        Command command = new UpdateMaterialClassificationTypeCommand();
        command.execute(materialClassificationTypePo);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除物料分类")
    public ResponseResult<String> deleteMaterialClassificationType(Long id){
        Command command = new DeleteMaterialClassificationTypeCommand();
        command.execute(id);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
